package Servlet.Blog.controller;

import Servlet.Blog.entity.User;
import Servlet.Blog.service.UserService;
import Servlet.Blog.service.impl.UserServiceImpl;
import Servlet.Blog.util.JSONUtil;
import com.mysql.cj.util.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


/**
 * @author liyy
 * @description
 * @time 2020/10/12 13:17
 */
@WebServlet(name = "UserServlet")
public class UserServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    UserService userService = null;

    public void init() throws ServletException
    {
        // 执行必需的初始化
        userService = new UserServiceImpl();
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //logger.info("userservlet");
        response.setCharacterEncoding("utf-8");

        List<User> userList1 = userService.queryUserList();

        String title1 = "用户";
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out1 = response.getWriter();

        String docType1 = "<!DOCTYPE html>\n";
        out1.println(docType1 +
                "<html> <head><title>" + title1 + "</title></head>\n" +
                "<body><table>");


        for (User u : userList1) {
            out1.println("id:"+u.getId());
            out1.println("name:"+u.getUsername());
            out1.println("status:"+u.getStatus());
        }
        out1.println("</table></body></html>");

//        String action = request.getParameter("action");
//        if(StringUtils.isNullOrEmpty(action)){
////设置返回编码格式,解决中文乱码
//            response.setCharacterEncoding("utf-8");
//
//            List<User> userList = userService.queryUserList();
//
//            String title = "用户";
//            response.setContentType("text/html;charset=UTF-8");
//            PrintWriter out = response.getWriter();
//
//            String docType = "<!DOCTYPE html>\n";
//            out.println(docType +
//                    "<html> <head><title>" + title + "</title></head>\n" +
//                    "<body><table>");
//
//
//            for (User u : userList) {
//                out.println("id:"+u.getId());
//                out.println("name:"+u.getUsername());
//                out.println("status:"+u.getStatus());
//            }
//            out.println("</table></body></html>");
//        }else if(action.equals("all")){
//            List<User> userList = userService.queryUserList();
//            //返回json数据格式
//            JSONArray jsonArr = JSONArray.fromObject(userList);//转成json数组
//            JSONObject jsonObjOut = new JSONObject();//声明json对象
//            //将集合转化成json格式
//            jsonObjOut.put("userList", jsonArr);
//            //调用写好的方法,返回json格式给前台
//            JSONUtil.returnJSON(request, response, jsonObjOut);
//        }
//        else if(action.equals("id")){
//            List<User> userList = userService.queryUserList();
//            //返回json数据格式
//            JSONArray jsonArr = JSONArray.fromObject(userList);//转成json数组
//            JSONObject jsonObjOut = new JSONObject();//声明json对象
//            //将集合转化成json格式
//            jsonObjOut.put("userList", jsonArr);
//            //调用写好的方法,返回json格式给前台
//            JSONUtil.returnJSON(request, response, jsonObjOut);
//        }else if(action.equals("user")){
//            //response.sendRedirect("user.html");
//            List<User> userList = userService.queryUserList();
//            //返回json数据格式
//            JSONArray jsonArr = JSONArray.fromObject(userList);//转成json数组
//            JSONObject jsonObjOut = new JSONObject();//声明json对象
//            //将集合转化成json格式
//            jsonObjOut.put("userList", jsonArr);
//            //调用写好的方法,返回json格式给前台
//            JSONUtil.returnJSON(request, response, jsonObjOut);
//        }



    }
}
